package com.company.huisuo.biz.activity

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.company.huisuo.R
import com.company.huisuo.base.BaseActivity
import com.company.huisuo.biz.adapter.ServiceStyleAdapter
import com.company.huisuo.biz.bean.RoomStyle
import com.company.huisuo.databinding.ActivityServiceStyleBinding
import com.company.huisuo.ext.toast
import com.company.huisuo.repository.RoomStyleRepository
import com.company.huisuo.repository.ServiceStyleRepository
import kotlinx.coroutines.launch

/**
 * 服务风格
 */
class ServiceStyleActivity : BaseActivity<ActivityServiceStyleBinding>() {

    private val mAdapter by lazy {
        ServiceStyleAdapter()
    }
    
    private val serviceStyleRepository by lazy {
        ServiceStyleRepository()
    }
    
    private val roomStyleRepository by lazy {
        RoomStyleRepository()
    }

    companion object {
        fun start(activity: Activity) {
            activity.startActivity(Intent(activity, ServiceStyleActivity::class.java))
        }
    }

    override fun initView() {
        mBinding.rcvList.apply {
            layoutManager =
                LinearLayoutManager(this@ServiceStyleActivity, RecyclerView.HORIZONTAL, false)
            adapter = mAdapter
        }
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mBinding.tvOk.setOnClickListener {
            "剧本选中成功".toast()
            finish()
        }
        mBinding.tvIntoGame.setOnClickListener {
            DanceGameActivity.start(this)
            finish()
        }
    }

    override fun initData() {
        loadServiceStyles()
    }
    
    /**
     * 加载服务风格数据
     */
    private fun loadServiceStyles() {
        lifecycleScope.launch {
            try {
                val result = roomStyleRepository.getRoomStyleList()
                if (result.isSuccess) {
                    val roomStyles = result.getOrThrow()
                    if (roomStyles.isNotEmpty()) {
                        // 对roomStyles进行循环处理，修改字段

                        mAdapter.submitList(roomStyles)
                    } else {
                        // 如果API返回空数据，使用本地默认数据作为fallback
                        showDefaultData()
                    }
                } else {
                    // 网络请求失败，使用本地默认数据作为fallback
                    showDefaultData()
                    "网络请求失败，显示默认数据".toast()
                }
            } catch (e: Exception) {
                // 异常处理，使用本地默认数据作为fallback
                showDefaultData()
                "数据加载失败，显示默认数据".toast()
            }
        }
    }
    
    /**
     * 显示默认数据（fallback方案）
     */
    private fun showDefaultData() {
        val list = arrayListOf<RoomStyle>()
        list.add(RoomStyle("1", "RS0001", "航空舞会", "", null, null,null))
        list.add(RoomStyle("2", "RS0002", "宫廷夜宴", "", null, null,null))
        list.add(RoomStyle("3", "RS0003", "校园自习", "", null, null,null))
        list.add(RoomStyle("4", "RS0004", "私人护士", "", null, null,null))
        list.add(RoomStyle("5", "RS0005", "航空舞会", "", null, null,null))
        
        // 对默认数据也进行相同的循环处理
        val processedList = list.mapIndexed { index, roomStyle ->
            roomStyle.copy(
                styleName = "${index + 1}. ${roomStyle.styleName}",
                styleCover = if (roomStyle.styleCover.isNullOrEmpty()) {
                    "https://example.com/default-style-cover.jpg"
                } else {
                    roomStyle.styleCover
                },
                clothingStyleMaterial = roomStyle.clothingStyleMaterial ?: "默认服装材质",
                roomMaterial = roomStyle.roomMaterial ?: "默认房间材质"
            )
        }
        mAdapter.submitList(processedList)
    }
}